/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * uiChartViewThreads.java
 *
 * Created on Jun 25, 2012, 9:55:14 AM
 */

package taskspydelux;

import com.skstudio.report.ReportThreads;
import com.skstudio.reportview.ReporterProcessThread;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import taskspydelux.uiChartView.uiChartViewListener;

/**
 *
 * @author skstudio
 */
public class uiChartViewThreads extends uiRootPanel  implements uiChartViewListener {

    /** Creates new form uiChartViewThreads */
    public uiChartViewThreads() {
        initComponents();
        getChart().setEventListener(this);
        getChart().setScrollBar(jScrollBar1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        infoButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        uicvChart = new uiChartView();
        jScrollBar1 = new javax.swing.JScrollBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        uicvThreadsInfo = new javax.swing.JTextArea();

        setPreferredSize(new java.awt.Dimension(642, 170));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setBackground(new java.awt.Color(0, 0, 0));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Prcoess's Threads View");
        jLabel1.setName("jLabel1"); // NOI18N

        closeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/taskspydelux/resources/icon_close.png"))); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        infoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/taskspydelux/resources/icon_about.png"))); // NOI18N
        infoButton.setName("infoButton"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 610, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(infoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(infoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        add(jPanel1);

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.Y_AXIS));

        uicvChart.setName("uicvChart"); // NOI18N

        javax.swing.GroupLayout uicvChartLayout = new javax.swing.GroupLayout(uicvChart);
        uicvChart.setLayout(uicvChartLayout);
        uicvChartLayout.setHorizontalGroup(
            uicvChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 391, Short.MAX_VALUE)
        );
        uicvChartLayout.setVerticalGroup(
            uicvChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 110, Short.MAX_VALUE)
        );

        jPanel3.add(uicvChart);

        jScrollBar1.setOrientation(javax.swing.JScrollBar.HORIZONTAL);
        jScrollBar1.setEnabled(false);
        jScrollBar1.setName("jScrollBar1"); // NOI18N
        jScrollBar1.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                jScrollBar1AdjustmentValueChanged(evt);
            }
        });
        jPanel3.add(jScrollBar1);

        jPanel2.add(jPanel3);

        jScrollPane1.setMaximumSize(new java.awt.Dimension(250, 32767));
        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setPreferredSize(new java.awt.Dimension(250, 92));

        uicvThreadsInfo.setColumns(20);
        uicvThreadsInfo.setEditable(false);
        uicvThreadsInfo.setLineWrap(true);
        uicvThreadsInfo.setRows(5);
        uicvThreadsInfo.setText("Click on the left chart to view threads CPU usage information break down");
        uicvThreadsInfo.setWrapStyleWord(true);
        uicvThreadsInfo.setName("uicvThreadsInfo"); // NOI18N
        jScrollPane1.setViewportView(uicvThreadsInfo);

        jPanel2.add(jScrollPane1);

        add(jPanel2);
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
      setVisible(false);
    }//GEN-LAST:event_closeButtonActionPerformed

    private void jScrollBar1AdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {//GEN-FIRST:event_jScrollBar1AdjustmentValueChanged
        getChart().onScroll(evt.getValue());
}//GEN-LAST:event_jScrollBar1AdjustmentValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JButton infoButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollBar jScrollBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel uicvChart;
    private javax.swing.JTextArea uicvThreadsInfo;
    // End of variables declaration//GEN-END:variables

    @Override
    public uiChartView getChart() {
        return (uiChartView)uicvChart;
    }

    public void onSelectionChanged(final int curCol) {
        if (curCol == -1) {
            uicvThreadsInfo.setText("Click on the left chart to view threads CPU usage information break down");
            uicvThreadsInfo.setWrapStyleWord(true);
            uicvThreadsInfo.setLineWrap(true);
            return;
        }

        final ReporterProcessThread reProcThreads = (ReporterProcessThread) getChart().getViewController();
        ReportThreads reThreads = reProcThreads.getThreads();

        ArrayList<Integer> matchedThreads = new ArrayList<Integer>();
        for (int ii = 0; ii < reProcThreads.getReportRows(); ii++) {
            int cpu = reProcThreads.getEntry(ii, curCol);
            if (cpu == -1) {
                continue; //the thread is not valid at current time
            }
            matchedThreads.add(ii);
        }
        Collections.sort(matchedThreads, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                int cpu1 = reProcThreads.getEntry(o1, curCol);
                int cpu2 = reProcThreads.getEntry(o2, curCol);
                return cpu2 - cpu1;
            }
        });

        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Total %d threads at the %d seconds\n\n", matchedThreads.size(), curCol));
        for (Integer row : matchedThreads) {
            long tid = reThreads.mDataList.get(row).getTid();
            String name = reThreads.mNameMap.get(tid);
            int cpu = reProcThreads.getEntry(row, curCol);
            String line = String.format("%s(id %d), %d%%\n", name, tid, cpu);
            sb.append(line);
        }
        uicvThreadsInfo.setLineWrap(false);
        uicvThreadsInfo.setText(sb.toString());
        uicvThreadsInfo.setCaretPosition(0);
    }
}
